package mycoffee.order.task;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import mycoffee.order.activity.MyCoffeeApplication;
import mycoffee.order.database.DatabaseConnection;
import mycoffee.order.model.Area;
import mycoffee.order.model.Table;
import mycoffee.order.utils.LogUtils;
import mycoffee.order.xmlhandler.AreasHandler;
import mycoffee.order.model.Food;

import android.content.Context;
import android.util.Log;

public class AreasTask extends AbstractTask<Void, Void, ArrayList<Area>> {
	private static final String TAG = "AreasTask";
	DatabaseConnection dbCon = MyCoffeeApplication.mDatabaseConnection;
	public AreasTask(Context context,
			IDataEventHandler<ArrayList<Area>> dataEventHandler) {
		super(context, dataEventHandler);
	}

	@Override
	protected ArrayList<Area> doExecute() {
		ArrayList<Area> result = this.loadDataFromDatabase();
		if (result == null) {
			result = this.loadDataFromXml();
		}
		
		return result;
	}

	private ArrayList<Area> loadDataFromDatabase() {
		Log.d(TAG, "In::loadDataFromDatabase");
		Area mCurrentArea = null;
		ArrayList<Area> result = dbCon.getListArea();
		ArrayList<Table> tables = null;
		Table currentTable = null;
		try {
			for ( int i = 0; i < result.size(); i++ ) {
				mCurrentArea = result.get(i);
				tables = dbCon.getListTable(mCurrentArea.getId());

				for ( int j = 0; j < tables.size(); j++ ) {
					currentTable = tables.get(j);
					currentTable.setAreaName(mCurrentArea.getName());
				}

				mCurrentArea.setTables(tables);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		Log.d(TAG, "Out::loadDataFromDatabase");
		return result;
	}
	
	private ArrayList<Area> loadDataFromXml() {
		ArrayList<Area> result = null;
		// read food.xml file.
		InputStream stream = null;
		try {
			// read data from assets/forecast.xml.
			stream = mContext.getAssets().open("xml/areas.xml");

			if (!getShouldCancel() && stream != null) {
				SAXParserFactory spf = SAXParserFactory.newInstance();

				SAXParser sp;
				AreasHandler areasHandler = new AreasHandler();

				try {
					sp = spf.newSAXParser();
					XMLReader xr = sp.getXMLReader();
					xr.setContentHandler(areasHandler);
					xr.parse(new InputSource(stream));
					result = areasHandler.getResult();
								

				} catch (ParserConfigurationException e) {
					LogUtils.logError(TAG, "ParserConfigurationException : "
							+ e.getMessage());
				} catch (SAXException e) {
					LogUtils.logError(TAG, "SAXException : " + e.getMessage());
				} catch (IOException e) {
					LogUtils.logError(TAG, "IOException : " + e.getMessage());
				}
			}
		} catch (IOException e) {
			//
		} finally {
			try {
				// close the stream.
				if (stream != null) {
					stream.close();
					stream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}
